//
//  HSYBillViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/7.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYBillViewController.h"
#import "HSYBillRecordsModel.h"
#import "HSYBillRecordCell.h"
#import "HSYGetBillRecordsRequest.h"

static NSString * const reuseIdentifier = @"cellReuseIdentifier";

@interface HSYBillViewController ()

@property(nonatomic,strong)HSYBillRecordsModel *dataModel;

@end

@implementation HSYBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.titleButton setTitle:@"收款记录" forState:UIControlStateNormal];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView registerNib:[UINib nibWithNibName:@"HSYBillRecordCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];

    [self hsy_headerBeginRefreshing];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)hsy_showEmptyImage{
    return YES;
}

-(void)hsy_loadServerData{
    
    [super hsy_loadServerData];
    if (self.pageIdex == 0) {
        self.pageIdex = self.pageIdex + 1;
    }
    
    HSYGetBillRecordsRequest *task = [[HSYGetBillRecordsRequest alloc] init];
    
    task.accountId = [HSYUserCenter instatce].appAuUser.accountID;
    task.pageNo = self.pageIdex;
    task.pageSize = self.pageStep;
    
    __weak typeof(self)weakSelf = self;

    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [weakSelf hsy_headerEndRefreshing];
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            HSYBillRecordsModel *recordsModel = [HSYBillRecordsModel mj_objectWithKeyValues:response.encryptDataResult];
            
            if (weakSelf.pageIdex != 1) { //为1是为下拉刷新
                [weakSelf.dataModel.records addObjectsFromArray:recordsModel.records];
                recordsModel.records = weakSelf.dataModel.records;
                
            }
            weakSelf.dataModel = recordsModel;

            if (recordsModel.totalPage <= weakSelf.pageIdex) {//没有更多数据
                [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusNoMoreData];
            }else{
                [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusDefault];
            }
            
        }else{
            [weakSelf showMessage:response.resultMessage];
            [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusNoMoreData];
        }
    } failure:^(NSError *error) {
        [weakSelf hsy_headerEndRefreshing];
        [weakSelf showMessage:@"请求超时"];
        [weakSelf hsy_loadServerDataComplete:kDataLoadCompleteStatusFailed];

    }];
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataModel.records.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 76.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HSYBillRecordModel *model = self.dataModel.records[indexPath.row];
    
    HSYBillRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.amountLabel.text = model.amountDetailString;
    cell.channelLabel.text = model.channelDetailString;
    cell.codeLabel.text = [NSString stringWithFormat:@"(%@)",model.code];
    cell.dateLabel.text = [NSString dateStringFrom1970:model.time/1000];
    cell.transactionStatusLabel.text = model.msg;

    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
